package com.cloud.pole.user.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.pole.api.user.BaseUserApi;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.RegexUtils;
import com.cloud.pole.user.constant.UserCommonConstant;
import com.cloud.pole.user.model.entity.BaseUser;
import com.cloud.pole.user.service.AuthService;
import com.cloud.pole.user.service.UserService;

/**
 * 不要登录验证的接口都写再这个里面
 * @author ThinkPad
 *
 */
@RestController
@RequestMapping("auth/front")
@Slf4j
public class AuthController {
 
    private String tokenHeader = CommonConstants.AUTH_USER_TOKEN_HEADER;
    
    @Autowired
    private AuthService authService;
    
    @Autowired
    private UserService userService;
    
    
    @RequestMapping(value = "login")
    public ApiRestResponse<BaseUserApi> login(
    		@RequestParam(name = "userName", defaultValue = "") String userName,
    		@RequestParam(name = "userPwd", defaultValue = "") String userPwd,
    		@RequestParam(name = "openId", defaultValue = "") String openId) throws Exception {
        try {
			ParamException.err(StringUtils.isBlank(userName), "用户名不能为空");
			ParamException.err(StringUtils.isBlank(userPwd), "密码不能为空");
			BaseUserApi userVo = authService.login(userName, userPwd, openId);
	        return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userVo);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error("登录错误{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    @RequestMapping(value = "smsLogin")
    public ApiRestResponse<BaseUserApi> smsLogin(
    		@RequestParam(name = "mobile", defaultValue = "", required = true) String mobile,
    		@RequestParam(name = "mobileArea", defaultValue = "86", required = true) String mobileArea,
    		@RequestParam(name = "verifyCode", defaultValue = "", required = true) String verifyCode,
    		@RequestParam(name = "openId", defaultValue = "") String openId) throws Exception {
        try {
			ParamException.err(StringUtils.isBlank(mobile), "手机号不能为空");
			ParamException.err(StringUtils.isBlank(mobileArea), "手机号归属地不能为空");
			ParamException.err(StringUtils.isBlank(verifyCode), "短信验证码不能为空");
			BaseUserApi userVo = authService.smsLogin(mobile, mobileArea, verifyCode, openId);
	        return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userVo);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error("登录错误{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    

	/**
	 * 小程序绑定手机号
	 */
    @RequestMapping(value = "wxUserBind")
    public ApiRestResponse<BaseUserApi> wxUserBind(
    		@RequestParam(defaultValue = "") String mobile,
    		@RequestParam(defaultValue = "86") String mobileArea,
    		@RequestParam(defaultValue = "") String openId,
    		@RequestParam(defaultValue = "1") String userType) throws Exception {
        try {
			ParamException.err(StringUtils.isBlank(mobile), "手机号不能为空");
			ParamException.err(StringUtils.isBlank(mobileArea), "手机号归属地不能为空");
			ParamException.err(StringUtils.isBlank(openId), "openId不能为空");

			BaseUserApi userVo = authService.wxUserBind(mobile, mobileArea, openId, Integer.parseInt(userType));
	        return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userVo);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error("登录错误{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    @RequestMapping(value = "userTokenLogin")
    public ApiRestResponse<BaseUserApi> userTokenLogin(@RequestHeader(defaultValue = "") String token) throws Exception {
        try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");
			BaseUserApi userVo = authService.userTokenLogin(token);
	        return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userVo);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error("登录错误{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    /**
     * 通过微信openId登录
     * 需要
     * 1、保存微信信息
     * 2、绑定手机
     * 3、授权登录
     * @param openId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "wxLogin")
    public ApiRestResponse<BaseUserApi> wxLogin(
    		@RequestParam(name = "openId", defaultValue = "") String openId) throws Exception {
        try {
			ParamException.err(StringUtils.isBlank(openId), "openId不能为空");
			BaseUserApi userVo = authService.wxLogin(openId);
	        return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userVo);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error("登录错误{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    @RequestMapping(value = "forgetLoginPwd")
    public ApiRestResponse<BaseUserApi> forgetLoginPwd(
    		@RequestParam(defaultValue = "") String mobile,
    		@RequestParam(defaultValue = "86") String mobileArea,
    		@RequestParam(defaultValue = "") String verifyCode,
    		@RequestParam(defaultValue = "") String newLoginPwd) throws Exception {
        try {
			ParamException.err(StringUtils.isBlank(mobile), "手机号不能为空");
			ParamException.err(StringUtils.isBlank(mobileArea), "手机号归属地不能为空");
			ParamException.err(StringUtils.isBlank(verifyCode), "短信验证码不能为空");
			ParamException.err(StringUtils.isBlank(newLoginPwd), "新登录密码不能为空");
			if(!RegexUtils.isMobileNo(mobile, mobileArea)) {
				ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, "手机号格式错误");
			}
			userService.forgetLoginPwd(mobile, mobileArea, verifyCode, newLoginPwd);
			
	        return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, null);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error("错误{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    @RequestMapping(value = "forgetPayPwd")
    public ApiRestResponse<BaseUserApi> forgetPayPwd(
    		@RequestParam(name = "mobile", defaultValue = "", required = true) String mobile,
    		@RequestParam(name = "mobileArea", defaultValue = "86", required = true) String mobileArea,
    		@RequestParam(name = "verifyCode", defaultValue = "", required = true) String verifyCode,
    		@RequestParam(name = "newPayPwd", defaultValue = "", required = true) String newPayPwd) throws Exception {
        try {
			ParamException.err(StringUtils.isBlank(mobile), "手机号不能为空");
			ParamException.err(StringUtils.isBlank(mobileArea), "mobileArea不能为空");
			ParamException.err(StringUtils.isBlank(verifyCode), "verifyCode不能为空");
			ParamException.err(StringUtils.isBlank(newPayPwd), "newPayPwd不能为空");
			
			if(!RegexUtils.isMobileNo(mobile, mobileArea)) {
				ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, "手机号格式错误");
			}
			userService.forgetPayPwd(mobile, mobileArea, verifyCode, newPayPwd);
	        return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, null);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error("错误{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    /**
        * 通用用户注册
     * @param mobile
     * @param verifyCode
     * @param loginPwd
     * @param mobileArea
     * @param clientType
     * @return
     * @throws Exception
     */
	@ResponseBody
	@RequestMapping(value = "/userRegister")
	public ApiRestResponse<BaseUserApi> userRegister(
			@RequestParam(defaultValue = "") String mobile,  
			@RequestParam(defaultValue = "86") String mobileArea,
			@RequestParam(defaultValue = "") String userName,  
			@RequestParam(defaultValue = "") String verifyCode, 
			@RequestParam(defaultValue = "") String loginPwd, 
			@RequestParam(defaultValue = BaseUser.USER_ORIGIN_TAG_ELSE) String originTag,
			@RequestHeader(defaultValue = "0") int clientType)throws Exception{
		try {
			ParamException.err(StringUtils.isBlank(mobile), "手机号不能为空");
			ParamException.err(StringUtils.isBlank(userName), "登录账号不能为空");
			ParamException.err(StringUtils.isBlank(verifyCode), "验证码不能为空");
			ParamException.err(StringUtils.isBlank(loginPwd), "密码不能为空");
			ParamException.err(StringUtils.isBlank(mobileArea), "手机号归属地不能为空");
			ParamException.err(StringUtils.isBlank(mobileArea), "手机号归属地不能为空");
			if(!RegexUtils.isMobileNo(mobile, mobileArea)) {
				ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, "手机号格式错误");
			}
			BaseUserApi baseUserVo = userService.userRegister(mobile, userName, verifyCode, loginPwd, mobileArea, "", originTag,  clientType);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, baseUserVo);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE +"{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}
	
	/**
	 * 微信注册
	 * @param mobile
	 * @param verifyCode
	 * @param loginPwd
	 * @param mobileArea
	 * @param openId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/userWxRegister")
	public ApiRestResponse<BaseUserApi> userWxRegister(
			@RequestParam(defaultValue = "") String mobile,
			@RequestParam(defaultValue = "86") String mobileArea,
			@RequestParam(defaultValue = "") String userName,  
			@RequestParam(defaultValue = "") String verifyCode, 
			@RequestParam(defaultValue = "") String loginPwd, 
			@RequestParam(defaultValue = "") String openId)throws Exception{
		try {
			ParamException.err(StringUtils.isBlank(mobile), "手机号不能为空");
			ParamException.err(StringUtils.isBlank(userName), "登录账号不能为空");
			ParamException.err(StringUtils.isBlank(verifyCode), "验证码不能为空");
			ParamException.err(StringUtils.isBlank(loginPwd), "密码不能为空");
			ParamException.err(StringUtils.isBlank(mobileArea), "手机号归属地不能为空");
			ParamException.err(StringUtils.isBlank(openId), "openId不能为空");
			if(!RegexUtils.isMobileNo(mobile, mobileArea)) {
				ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, "手机号格式错误");
			}
			BaseUserApi baseUserVo = userService.userRegister(mobile, userName, verifyCode, loginPwd, mobileArea, openId, BaseUser.USER_ORIGIN_TAG_WEIXIN,  UserCommonConstant.CLIENT_TYPE_ELSE);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, baseUserVo);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE +"{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}
}
